<template>
  <div>
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <div style="text-align: center; margin-top: 20px">
      <el-button>取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting.js'
import { assignRoles } from '@/api/employees'
export default {
  name: '',
  //   父组件的传值，子组件接收
  props: {
    roleIds: {
      type: Array,
      default: () => {}
    },
    userId: {
      type: String,
      default: () => {}
    }
  },
  data() {
    return {
      roleParams: { page: 1, pagesize: 20 },
      checkList: [],
      roleList: []
    }
  },
  computed: {},
  //   监听，当触发有变化时，newVal会有值
  watch: {
    roleIds: {
      handler(newVal) {
        this.checkList = newVal
      },
      immediate: true
    }
  },
  created() {
    this.getRolesList()
    // 获取用户当前所拥有的角色
    // this.checkList = this.roleIds
  },
  methods: {
    //   获取角色列表
    async getRolesList() {
      const res = await getRoles(this.roleParams)
      this.roleList = res.data.rows
    },
    // 点击提交
    async submit() {
      const res = await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$emit('success')
      console.log('assignRoles', res)
    }
  }
}
</script>
<style lang="less" scoped></style>
